package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.UserDao.AdminQueryVO;
import ebusiness.ebusiness.Dao.UserDao.UserQueryVO;
import ebusiness.ebusiness.Entity.UserEntity.Administrator;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Entity.UserEntity.TempUser;
import ebusiness.ebusiness.Mapper.JsonUtil.StatusUtil;
import ebusiness.ebusiness.Mapper.JsonUtil.strTOjson;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class LoginController3 {
    @RequestMapping(path = WebPath.LOGIN,method = RequestMethod.POST)
    public String Login(HttpSession session,String name,String password) throws Exception {
    	
       /* name=us.name;
        password=us.password;*/
        UserQueryVO userQueryVO = new UserQueryVO();
        RegisteUser user=null;

             user= userQueryVO.getRegisteUserByName(name);
        

        if(user == null) {
            AdminQueryVO adminQueryVO = new AdminQueryVO();
            Administrator admin = adminQueryVO.getAdminByName(name);
            if (admin == null) {
                //找不到该用户
                return StatusUtil.LoginFail;
            } else {
                String resname = admin.getName();
                String respassword = admin.getPassword();
                if (name.equals(resname) && password.equals(respassword)) {
                    session.setAttribute("admin", admin);
                    return  StatusUtil.LoginSuccess;
                } else {
                    //密码错误
                    return StatusUtil.LoginWrongPassWord;
                }
            }
        }
        else{
            String resname = user.getName();
            String respassword = user.getPassword();

//			 MessageDigest md = MessageDigest.getInstance("MD5");
//	         byte[] inputData = password.getBytes();
//	         md.update(inputData);
//	         BigInteger bigInteger = new BigInteger(md.digest());
//			 password = bigInteger.toString(32);

            if (name.equals(resname) && password.equals(respassword)) {
                session.setAttribute("user", user.getId());
                
                return StatusUtil.LoginSuccess;
            } else {
                //密码错误
                return StatusUtil.LoginFail;
            }

        }
    }
}
